import logging

from main.config import driver_path, chrome_options
from selenium import webdriver

from utils.image_util import download_save_img

browser = webdriver.Chrome(executable_path=driver_path, options=chrome_options)
browser.implicitly_wait(20)


def download(url):
    browser.get(url)
    name = browser.find_element_by_css_selector('#main-container > div.row.nav-row > div > span > a:nth-child(3)').text
    catalog_tabs = browser.find_elements_by_css_selector('#main-container > div.text-list-html > div > ul > li > a')

    catalog = list()  # 目录
    for tab in catalog_tabs:
        title = tab.get_attribute('title')
        if title != '':
            catalog.append((title, tab.get_attribute('href')))
            logging.info('catalog add {}:{} \t{}'.format(title, tab.get_attribute('href'), len(catalog)))

    next_page_tab = \
    browser.find_elements_by_css_selector('#main-container > div.text-list-html > div > div.pagination > a')[-2]
    while (next_page_tab.text == '下一页' and next_page_tab.get_attribute('href') != 'javascript:;'):
        browser.get(next_page_tab.get_attribute('href'))
        catalog_tabs = browser.find_elements_by_css_selector('#main-container > div.text-list-html > div > ul > li > a')
        for tab in catalog_tabs:
            title = tab.get_attribute('title')
            if title != '':
                catalog.append((title, tab.get_attribute('href')))
                logging.info('catalog add {}:{} \t{}'.format(title, tab.get_attribute('href'), len(catalog)))

        next_page_tab = \
        browser.find_elements_by_css_selector('#main-container > div.text-list-html > div > div.pagination > a')[-2]

    # 收集每个 catalog 中的数据
    for title, href in catalog:
        browser.get(href)
        img_tabs = browser.find_elements_by_css_selector('#main-container > div.content > img')
        for tab in img_tabs:
            src = tab.get_attribute('data-original')
            download_save_img(src, '{}/{} {}'.format(name, title, src.split('/')[-1]))

    print()


if __name__ == '__main__':
    id = '*'
    prefix = 'https://******/**/'
    download(prefix + id)
